package com.tbi.graphcolouring.testers;

import java.util.Collections;
import java.util.ArrayList;

import com.tbi.graphcolouring.utils.Random;

public class SelectorTest {
	public static void main(String[] args) {
		//population initialization & evaluation
		final int POP_SIZE = 100;
		
		ArrayList<Float> population = new ArrayList<>();
		for(int i=0; i<POP_SIZE; i++){
			population.add((float)Random.uniform());
		}
		
		
		
		//selector begins
		Collections.sort(population);
		
		System.out.println("Initial population");
		for(Float d : population){
			System.out.print(d + ", ");
		}
		
		ArrayList<Float> newPopulation = new ArrayList<>();
		
		for(int i=0; i<POP_SIZE/2; i++){
			newPopulation.add(population.remove(
					(int)(Math.abs(Random.gaussian(0., 0.2)) * (POP_SIZE - i))
				));
		}

		Collections.sort(newPopulation);
		
		System.out.println("\nNext generation population");
		for(Float d : newPopulation){
			System.out.print(d + ", ");
		}
		
		//crossover begins
		for(int i=0; i<POP_SIZE/2; i++){
			Float entity1 = newPopulation.get((int)(Math.abs(Random.gaussian(0., 0.2)) * (POP_SIZE/2 - i)));
			Float entity2 = newPopulation.get((int)(Math.abs(Random.gaussian(0., 0.2)) * (POP_SIZE/2 - i)));
			
			newPopulation.add((entity1 + entity2)/2f);
		}
		
		Collections.sort(newPopulation);
		
		System.out.println("\nNext generation population after crossover");
		for(Float d : newPopulation){
			System.out.print(d + ", ");
		}
		
		//mutation begins
		for(int i=0; i<POP_SIZE/10; i++){
			Float entity = newPopulation.remove(((int)Random.uniform(0, POP_SIZE)));
			if(Random.checkSuccess(0.5)){
				entity += (float)Random.uniform();
			}else{
				entity -= (float)Random.uniform();
			}
			newPopulation.add(entity);
		}
		
		Collections.sort(newPopulation);
		
		System.out.println("\nNext generation population after mutation");
		for(Float d : newPopulation){
			System.out.print(d + ", ");
		}

	}
}
